package com.longfor.dragonshard.config;

import com.longfor.dragonshard.security.interceptor.AdminAccessTokenInterceptor;
import com.longfor.dragonshard.security.interceptor.WebAccessTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author chi.zhang
 * @version v1.0
 * @create 2017-10-08 上午9:38
 **/
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminAccessTokenInterceptor())
                .addPathPatterns("/admin/api/**");
        registry.addInterceptor(webAccessTokenInterceptor())
                .addPathPatterns("/web/api/**");
        super.addInterceptors(registry);
    }

    @Bean
    AdminAccessTokenInterceptor adminAccessTokenInterceptor() {
        return new AdminAccessTokenInterceptor();
    }

    @Bean
    WebAccessTokenInterceptor webAccessTokenInterceptor() {
        return new WebAccessTokenInterceptor();
    }

}

